import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 32309
 * Date: 2025-07-05
 * Time: 18:34
 */
public class demo_insert {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            // 1. 获取数据库连接
            connection = DB.getConnection();

            // 2. 定义sql
            String sql = "insert into student(sn,name,mail,class_id) values (?,?,?,?)";

            // 3. 定义预处理对象
            preparedStatement = connection.prepareStatement(sql);

            // 4. 接收用户输入
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入学号：");
            String stu_sn = scanner.nextLine();
            System.out.println("请输入姓名：");
            String stu_name = scanner.nextLine();
            System.out.println("请输入邮箱：");
            String stu_mail = scanner.nextLine();
            System.out.println("请输入班级编号：");
            long classId = scanner.nextLong();

            // 5. 用真实数据填充占位符
            preparedStatement.setString(1,stu_sn);
            preparedStatement.setString(2,stu_name);
            preparedStatement.setString(3,stu_mail);
            preparedStatement.setLong(4,classId);

            // 6. 执行sql获取结果
            int row = preparedStatement.executeUpdate();
            // 7. 判断结果
            if (row == 1) {
                System.out.println("插入成功！！！");
            }else {
                System.out.println("插入失败");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DB.close(resultSet,preparedStatement,connection);
        }
    }
}
